package com.yanqu.road.server.manger.activity.ad;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ad.WatchTimesReward;
import com.yanqu.road.entity.config.system.PropertyWeightItem;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

public class AdActivityConfig {
    private int activityId;                 //活动编号
    private int coolCdTime;                 //观看冷却CD时间，单位s
    private int dailyMaxWatchTimes;         //每日观看广告次数上限
    private String singleRewardParam;       //普通奖励
    private Map<Integer, List<PropertyWeightItem>> singleRewardMap;
    private String firstReward;             //第一次奖励
    private int firstMaxWatchTimes;         //第一次最大观看次数
    private List<WatchTimesReward> firstRewardList;         //第一次奖励
    private String secondReward;            //第二次观看奖励
    private List<WatchTimesReward> secondRewardList;        //第二次奖励

    public AdActivityConfig(ActivityInfo activityInfo, Map<String, ActivityConfig> activityConfigMap) {
        activityId = activityInfo.getActivityId();
        coolCdTime = ActivityHelper.getActivityConfigIntValue(activityConfigMap, eActivityConfigType.WATCH_AD_COOL_CD);
        dailyMaxWatchTimes = ActivityHelper.getActivityConfigIntValue(activityConfigMap, eActivityConfigType.WATCH_AD_NUM_MAX);
        singleRewardParam = ActivityHelper.getActivityConfigValue(activityConfigMap, eActivityConfigType.WATCH_AD_SINGLE_REWARD);
        firstReward = ActivityHelper.getActivityConfigValue(activityConfigMap, eActivityConfigType.WATCH_AD_FIRST_REWARD);
        secondReward = ActivityHelper.getActivityConfigValue(activityConfigMap, eActivityConfigType.WATCH_AD_SECOND_REWARD);
        singleRewardMap = parseSingleRewardMap(singleRewardParam);
        parseFirstReward(firstReward);
        secondRewardList = parseWatchTimesReward(secondReward);
    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public int getCoolCdTime() {
        return coolCdTime;
    }

    public void setCoolCdTime(int coolCdTime) {
        this.coolCdTime = coolCdTime;
    }

    public int getDailyMaxWatchTimes() {
        return dailyMaxWatchTimes;
    }

    public void setDailyMaxWatchTimes(int dailyMaxWatchTimes) {
        this.dailyMaxWatchTimes = dailyMaxWatchTimes;
    }

    public String getSingleRewardParam() {
        return singleRewardParam;
    }

    public void setSingleRewardParam(String singleRewardParam) {
        this.singleRewardParam = singleRewardParam;
    }

    public String getFirstReward() {
        return firstReward;
    }

    public void setFirstReward(String firstReward) {
        this.firstReward = firstReward;
    }

    public int getFirstMaxWatchTimes() {
        return firstMaxWatchTimes;
    }

    public void setFirstMaxWatchTimes(int firstMaxWatchTimes) {
        this.firstMaxWatchTimes = firstMaxWatchTimes;
    }

    public String getSecondReward() {
        return secondReward;
    }

    public void setSecondReward(String secondReward) {
        this.secondReward = secondReward;
    }

    private Map<Integer, List<PropertyWeightItem>> parseSingleRewardMap(String configStr){
        Map<Integer, List<PropertyWeightItem>> dataMap = new HashMap<>();
        String[] configArr = configStr.split("\\|");
        for(int i = 0; i < configArr.length; i++){
            List<PropertyWeightItem> dataList = new ArrayList<>();
            String[] paramArr = configArr[i].split(";");
            for(String paramStr : paramArr){
                String[] weightArr = paramStr.split(",");
                PropertyWeightItem weightItem = new PropertyWeightItem();
                weightItem.setProperty(PropertyHelper.parseStringToProperty(weightArr[0]));
                weightItem.setWeight(StringUtils.string2intSafe(weightArr[1], 0));
                dataList.add(weightItem);
            }
            if(dataList.size() > 0){
                dataMap.put(i, dataList);
            }
        }
        return dataMap;
    }

    private void parseFirstReward(String firstReward){
        if(StringUtils.isNullOrEmpty(firstReward) || "0".equals(firstReward)){
            firstMaxWatchTimes = 0;
            firstRewardList = new ArrayList<>();
            return;
        }
        int maxTimes = 0;
        List<WatchTimesReward> dataList = parseWatchTimesReward(firstReward);
        if(dataList.size() > 0){
            WatchTimesReward lastData = dataList.get(dataList.size() - 1);
            maxTimes = lastData.getTimes();
        }
        firstMaxWatchTimes = maxTimes;
        firstRewardList = dataList;
    }

    private List<WatchTimesReward> parseWatchTimesReward(String configStr){
        List<WatchTimesReward> dataList = new ArrayList<>();
        if(StringUtils.isNullOrEmpty(configStr)){
            return dataList;
        }
        String[] timesArr = configStr.split("\\|");
        for(String timesStr : timesArr){
            String[] paramArr = timesStr.split("#");
            int times = StringUtils.string2intSafe(paramArr[0], 0);
            String reward = paramArr[1];
            WatchTimesReward data = new WatchTimesReward();
            data.setTimes(times);
            data.setReward(reward);
            dataList.add(data);
        }
        dataList.sort(Comparator.comparingInt(WatchTimesReward::getTimes));
        return dataList;
    }

    public Property getSingleReward(){
        Property reward = new Property();
        for(List<PropertyWeightItem> dataList : singleRewardMap.values()){
            reward.addProperty(RandomWeightConfigHelper.getPropertyWeightItem(dataList));
        }
        return reward;
    }

    public WatchTimesReward getFirstWatchTimesReward(int index){
        if(index >= 0 && index < firstRewardList.size()){
            return firstRewardList.get(index);
        }
        return null;
    }

    public WatchTimesReward getSecondWatchTimesReward(int index){
        if(index >= 0 && index < secondRewardList.size()){
            return secondRewardList.get(index);
        }
        return null;
    }

    public int getFirstRewardListSize(){
        return firstRewardList.size();
    }
}
